﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace GdNet.WebFramework.Domain.System
{
    public partial class Module : EntityHistory
    {
        private readonly IList<ModuleAction> _actions = new List<ModuleAction>();

        public virtual string Code { get; set; }
        public virtual string Name { get; set; }
        public virtual string Version { get; set; }
        public virtual string AssemblyName { get; set; }

        internal protected Module() { }

        public virtual ReadOnlyCollection<ModuleAction> Actions
        {
            get { return new ReadOnlyCollection<ModuleAction>(_actions); }
        }

        public virtual Module AddAction(ModuleAction action)
        {
            if (_actions.Contains(action) == false)
            {
                _actions.Add(action);
                action.ChangeModule(this);
            }
            return this;
        }

        public virtual ModuleAction GetActionByCode(string actionCode)
        {
            return _actions.FirstOrDefault(a => a.Code == actionCode);
        }
    }
}
